const query = require("./query");
const save = require("./save");
const handle = require("./handle");

function combination(info) {
  let class_header = handle.table.start(info);
  let class_footer = handle.table.end(info);
  query
    .column(info.name)
    .then((columns) => {
      let class_content = columns
        .map((column) => {
          return {
            comment: handle.column.comment(column),
            annotation: handle.column.annotation(column),
            content: handle.column.content(column),
            gset: handle.column.gset(column),
          };
        })
        .map((column) => `${column.comment}\n${column.annotation}\n${column.content}${column.gset}`)
        .join("\n");
      // 保存
      save.mapper(info.name);
      save.dao(info.name, `${class_header}\n${class_content}${class_footer}`);
    })
    .catch(console.error);
}

module.exports = {
  run: function () {
    // 获取所有表
    query
      .table()
      // 处理表
      .then((tables) =>
        // 循环处理
        tables.forEach(combination)
      )
      .catch(console.error);
  },
};
